package com.axjy.service.impl;

import com.axjy.mapper.MenuMapper;
import com.axjy.mapper.PermissionMapper;
import com.axjy.mapper.UserMapper;
import com.axjy.pojo.Menu;
import com.axjy.pojo.User;
import com.axjy.service.PermissionService;
import com.axjy.service.vo.Node;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * Created by asus on 2018/2/25.
 */
@Service("permissionService")
@Transactional
public class PermissionServiceImpl implements PermissionService {

    @Resource
    private PermissionMapper permissionMapper;

    @Resource
    private MenuMapper menuMapper;

    @Resource
    private UserMapper userMapper;


    @Override
    public Map<Menu, List<Menu>> createMenus(Integer userId) {
        Map<Menu, List<Menu>> map=new TreeMap<Menu,List<Menu>>();
        User user=userMapper.get(userId);
        Integer roleId=user.getRoleId();
        List<Integer> ids=  permissionMapper.searchMenuIds(roleId);
        for(Integer id:ids){
            Menu menu=menuMapper.get(id);
            if(menu.getPid()==null){
                List<Menu> children=new ArrayList<Menu>();
                map.put(menu,children);
                for(Integer id2:ids){
                    Menu m2=menuMapper.get(id2);
                    if(m2.getPid()!=null&&m2.getPid().equals(menu.getId())){
                        children.add(m2);
                    }
                }
            }
        }
        return map;
    }

    @Transactional(readOnly = true)
    @Override
    public List<Node> search4Tree(Integer roleId) {
        List<Integer> yunxu=permissionMapper.searchMenuIds(roleId);
        List<Node> level01=new ArrayList<Node>();
        List<Menu> menus=menuMapper.searchLevel01();
        for(Menu m01:menus){
            Node n01=new Node();
            n01.setIcon(m01.getIconSkin());
            n01.setId(m01.getId());
            n01.setText(m01.getName());
            if(yunxu.contains(m01.getId())){
                n01.getState().setChecked(true);
            }
            List<Menu> children=m01.getChildren();
            if(children!=null&&children.size()>0){
                List<Node> level02=new ArrayList<Node>();
                for(Menu m02:children){
                    Node n02=new Node();
                    n02.setIcon(m02.getIconSkin());
                    n02.setId(m02.getId());
                    n02.setText(m02.getName());
                    if(yunxu.contains(m02.getId())){
                        n02.getState().setChecked(true);
                    }
                    level02.add(n02);
                }
                n01.setNodes(level02);
            }
            level01.add(n01);
        }
        return level01;
    }

    @Override
    public void grant4Role(Integer roleId, Integer[] mids) {
            permissionMapper.del(roleId);
            if(mids!=null&&mids.length>0){
                for (Integer mid:mids){
                    permissionMapper.add(roleId,mid);
                }
            }
    }
}
